import java.lang.reflect.Method;
import java.util.Scanner;

//java -jar 16End6681.jar
public class Menu6681 {
    static String[] mainArgs = new String[0];//给main方法传参
    //主菜单项（一级菜单）：实验名称
    static String mainMenuItem[] = {"实验4数组的应用", "实验5方法与模块化设计",
            "实验06类的定义与使用","实验07构造方法与类的标准写法","实验08字符串初步",
            "实验09String与StringBuilder","实验10static关键字与工具类",
             "实验11类的继承关系","实验12Object与常用关键字","实验13异常处理",
            "实验14对象转型与多态","实验15抽象类与接口"};
    //二级菜单，每个实验的题目与对应的类名
    //实验4数组的应用
    static String menuItem4[][] = {
            {"商城库存清单程序设计", "java6681.lesson04.StoreList6681"},
            {"数组的最值与求和", "java6681.lesson04.Array6681"},
            {"编程题,数组的排序与查找", "java6681.lesson04.ArraySortAndSearch6681"},
            {"编程题,不重复的随机数", "java6681.lesson04.Datas6681"},
            {"附加题,抢红包", "java6681.lesson04.RedPacket6681"}
    };
    //实验5方法与模块化设计
    static String menuItem5[][] = {
            {"判断两个数据是否相等", "java6681.lesson05.DataEquals6681"},
            {"编程题,随机点名器", "java6681.module.CallName"},
            {"编程题,改进商城库存清单程序设计", "java6681.module.StoreList6681"},
            {"编程题,改进一组不重复随机数", "java6681.module.Datas6681"},
            {"附加题,命令行参数", "Calc6681"},
            {"附加题,菜单程序", "java6681.bank.menu.Menu6681"},
    };
    //实验06类的定义与使用
    static String menuItem6[][] = {
            {"阅读理解Example6681", "java6681.lesson06.Example6681"},
            {"定义并测试类：Person", "java6681.lesson06.Person6681Demo"},
            {"编程题,卡车类的定义与使用", "java6681.truck.UseTruck6681"},
            {"编程题,计算平均字符数", "java6681.truck.AverageCharacters6681"},
            {"附加题,小型家庭旅馆管理", "java6681.hotel.Hotel6681"}
    };
    //实验07构造方法与类的标准写法
    static String menuItem7[][] = {
            {"计算长方形面积和周长", "java6681.lesson07.Rectangle6681Demo"},
            {"计算面积", "java6681.lesson07.AreaTools6681Demo"},
            {"编程题,看电视", "java6681.tv.User6681"},
            {"附加题,某个区间的随机数", "java6681.random.Menu6681"}
    };
    //实验08字符串初步
    static String menuItem8[][] = {
            {"模拟登录", "java6681.lesson08.Logon6681"},
            {"字符串中的字符排序", "java6681.lesson08.StringSort6681"},
            {"判断数字串", "java6681.lesson08.NumberString6681"},
            {"编程题,剔除非数字", "java6681.analysis.GetValue6681"},
            {"编程题,大小写交换", "java6681.analysis.SwapUpLower6681"},
            {"附加题,改进登录", "java6681.login.DemoLogin6681"}
    };
    //实验09String与StringBuilder
    static String menuItem9[][] = {
            {"String与StringBuilder的比较", "java6681.lesson09.StudyStringBuilder6681"},
            {"验证手机号码", "java6681.lesson09.PhoneNumberString6681"},
            {"编程题,第一个和最后一个单词", "java6681.string.WordString6681"},
            {"编程题,密码的安全性与单元测试", "java6681.string.PasswordUnit6681"},
            {"附加题,自定义字符串拆分", "java6681.oursplit.OurSplit6681Test"}
    };
    //实验10static关键字与工具类
    static String menuItem10[][] = {
            {"小学数学真题", "java6681.lesson10.MathExercise6681"},
            {"统计工具类", "java6681.lesson10.Counter6681Test"},
            {"编程题,阅读理解：static关键字的应用场景", "java6681.single.Trade6681"},
            {"编程题,共饮同井水", "java6681.single.Village6681Demo"},
            {"编程题,单例设计模式", "java6681.single.DemoGameBoss6681"},
            {"附加题,模拟用户注册", "java6681.user.AddUser6681"}
    };
    //实验11类的继承关系
    static String menuItem11[][] = {
            {"用继承来复用代码", "java6681.lesson11.Demo6681"},
            {"编程题,发红包", "java6681.red.MainRedPacket6681"},
            {"附加题,员工管理", "java6681.salary.SalaryDemo6681"}
    };
    //实验12Object与常用关键字
    static String menuItem12[][] = {
            {"带包的命令行工具", "java6681.lesson12.Triangle6681"},
            {"编程题,银行账户类", "java6681.bank.BankDemo6681"},
            {"附加题,模拟银行业务", "java6681.bank.menu.BankMenu6681"}
    };
    //实验13异常处理
    static String menuItem13[][] = {
            {"异常语法练习,简单的异常处理", "java6681.lesson13.Exception6681_1"},
            {"异常语法练习,多重catch", "java6681.lesson13.Exception6681_2"},
            {"异常语法练习,finally", "java6681.lesson13.Exception6681_3"},
            {"学生年龄异常", "java6681.lesson13.ThrowDemo6681"},
            {"编程题,成绩异常", "java6681.mythrow.Exception6681_4"}
//            {"附加题,异常处理", "java6681.bank.menu.BankMenu6681"}
    };
    //实验14对象转型与多态
    static String menuItem14[][] = {
            {"多态的实现", "java6681.lesson14.ShapeDemo"},
            {"计算面积", "java6681.lesson14.ShapeDemo6681"},
            {"编程题,交通工具", "java6681.task.VehicleDemo6681"}
    };
    //实验15抽象类与接口
    static String menuItem15[][] = {
            {"阅读并实现：USB接口程序设计", "java6681.lesson15.usb.USBDemo6681"},
            {"计算成绩", "java6681.lesson15.score.AverageaDemo6681"},
            {"抽象方法与抽象类-课堂完成部分", "java6681.lesson14.ShapeDemo6681"},
            {"抽象方法与抽象类-编程题", "java6681.task.VehicleDemo6681"},
            {"编程题,运动员与教练", "java6681.coach.PersonDemo6681"},
            {"附加题,对象排序", "java6681.sort.SortDemo6681"}
    };
    //所有二级菜单存储在三维数组中（每个元素是一个二级菜单）
    //第一维的下标与主菜单对应，用于从主菜单进入二级菜单
    static String menu[][][] = {menuItem4, menuItem5,menuItem6,menuItem7,
            menuItem8,menuItem9,menuItem10,menuItem11,menuItem12,
            menuItem13,menuItem14,menuItem15};

    public static void main(String[] args) throws Exception {
        mainMenu();//进入主菜单
    }

    static void showMainMenu() {
        //显示主菜单
        System.out.println("==========212006681：张舒婷，实验目录==========");
        for (int i = 0; i < mainMenuItem.length; i++) {
            System.out.println("     " + (i + 1) + ".-------" + mainMenuItem[i]);
        }
        System.out.println("     0.---------结束");
        System.out.println("============== 2020年12月==============");
        System.out.print("请输入选择：");
    }

    static void mainMenu() throws Exception {
        //主菜单控制逻辑
        Scanner sc = new Scanner(System.in);
        while (true) {
            showMainMenu();//显示主菜单
            boolean flag = true;
            while (flag) {
                String select = sc.nextLine().trim();
                switch (select) {
                    case "1":
                    case "2":
                    case "3":
                    case "4":
                    case "5":
                    case "6":
                    case "7":
                    case "8":
                    case "9":
                    case "10":
                    case "11":
                    case "12":
                        //进入二级菜单
                        menu2(Integer.parseInt(select));
                        flag=false;
                        break;
                    case "0":
                        System.out.println("谢谢使用！");
                        System.exit(0);
                    default:
                        System.out.print("输入错误，请重新选择：");
                }
            }
            System.out.print("请按回车回到主菜单：");
            sc.nextLine();
        }
    }

    static void showMenu2(int k) {
        //显示二级菜单（某个实验中的题目清单）
        System.out.println("=======212006681：张舒婷【" + mainMenuItem[k - 1] + "】=======");
        for (int i = 0; i < menu[k - 1].length; i++) {
            System.out.println("     " + (i + 1) + ".-------" + menu[k - 1][i][0]);
        }

        System.out.println("     0.---------返回实验目录");
        System.out.println("==============2020年12月==============");
        System.out.print("请输入选择：");
    }

    static void menu2(int k) {
        //二级菜单的控制逻辑
        Scanner sc = new Scanner(System.in);
        int select = 1;
        while (select != 0) {
            showMenu2(k);//显示二级菜单
            boolean isBreak = false;
            while (!isBreak) {
                //循环直到输入正确的选项
                try {
                    select = Integer.parseInt(sc.nextLine());
                    if (select == 0) {
                        isBreak = true;//结束输入
                    } else if (select >= 1 && select <= menu[k - 1].length) {
                        isBreak = true;//结束输入
                        System.out.println("执行："+menu[k - 1][select-1][0]);
                        runMenuItem(menu[k - 1][select - 1][1]);
                        System.out.print("运行结束，请按回车回到【"+ mainMenuItem[k - 1]+"】");
                        sc.nextLine();
                    } else {
                        System.out.print("输入超出范围，请重新输入：");
                    }
                } catch (Exception e) {
                    System.out.print("输入格式错误，请重新输入：");
                }
            }
        }
    }

    static void runMenuItem(String className) {
        //用反射技术执行指定的程序
        Class<?> program;
        Method method;
        try {
            program = Class.forName(className);//加载类
            //得到main方法
            //第二个参数是main方法的参数类型
            method = program.getMethod("main", String[].class);
            //执行main方法
            //第一个参数是调用方法的实例对象，静态方法不需要指定实例对象
            //第二个参数是调用方法传递的实参
            method.invoke(null, (Object) mainArgs);
         } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
